package com.sl.biz.modular.process.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.biz.modular.process.controller.response.ProcessVersionVO;
import com.sl.biz.modular.process.dao.domain.dto.ProcessConfigDTO;
import com.sl.biz.modular.process.dao.domain.dto.ProcessVersionDTO;
import com.sl.biz.modular.process.dao.domain.entity.ProcessConfigDO;
import com.sl.biz.modular.process.dao.domain.entity.ProcessVersionContentDO;
import com.sl.biz.modular.process.dao.domain.entity.ProcessVersionDO;
import com.sl.biz.modular.process.dao.service.ProcessConfigDaoService;
import com.sl.biz.modular.process.dao.service.ProcessVersionContentDaoService;
import com.sl.biz.modular.process.dao.service.ProcessVersionDaoService;
import com.sl.biz.modular.process.service.ProcessVersionBizService;
import com.sl.common.enums.CommonDsiableFlagEnum;
import com.sl.core.domain.dto.SquareLakeLayout;
import com.sl.core.utils.JsonUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor
public class ProcessVersionBizServiceImpl implements ProcessVersionBizService {
    private final ProcessVersionDaoService versionDaoService;
    private final ProcessConfigDaoService configDaoService;
    private final ProcessVersionContentDaoService processVersionContentDaoService;

    @Override
    public List<ProcessVersionVO> list(ProcessVersionDO processVersionDO) {
        List<ProcessVersionDTO> list = versionDaoService.queryList(processVersionDO);
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<ProcessVersionVO> processVersionVOS = BeanUtil.copyToList(list, ProcessVersionVO.class);

        List<String> processIdList = processVersionVOS.stream().map(ProcessVersionDO::getProcessId).collect(Collectors.toList());

        List<ProcessConfigDTO> byIds = configDaoService.getByIds(processIdList);
        Map<String, ProcessConfigDTO> configMap = byIds.stream().collect(Collectors.toMap(v -> v.getId(), Function.identity(), (k1, k2) -> k1));

        for (ProcessVersionVO processVersionVO : processVersionVOS) {
            ProcessConfigDTO processConfigDTO = configMap.get(processVersionVO.getProcessId());
            Optional.ofNullable(processConfigDTO).ifPresent(v -> {
                processVersionVO.setProcessCode(v.getProcessCode());
                processVersionVO.setProcessName(v.getProcessName());
            });
        }
        return processVersionVOS;
    }

    @Override
    public String save(ProcessVersionDO addRequest) {

        ProcessConfigDO byId = configDaoService.getById(addRequest.getProcessId());
        if (ObjectUtil.isNull(byId)) {
            return null;
        }

        String id = addRequest.getId();
        if (StrUtil.isBlank(id)) {

            addRequest.setLatestFlag(0);
            addRequest.setDisableFlag(CommonDsiableFlagEnum.DISABLE_FLAG.getCode());
            id = IdUtil.getSnowflakeNextIdStr();
            addRequest.setId(id);
            //还要在保存一个
            ProcessVersionContentDO processVersionContentDO = new ProcessVersionContentDO();
            processVersionContentDO.setProcessId(addRequest.getProcessId());
            processVersionContentDO.setProcessVersion(addRequest.getProcessVersion());
            processVersionContentDO.setProcessVersionId(id);
            processVersionContentDaoService.save(processVersionContentDO);
            //如果 是新增的话 这里需要查询出最新版本，然后赋值
            ProcessVersionDO processVersionDO = new ProcessVersionDTO();
            processVersionDO.setProcessId(addRequest.getProcessId());
            List<ProcessVersionDTO> processVersionDTOS = versionDaoService.queryList(processVersionDO);

            if (CollUtil.isNotEmpty(processVersionDTOS)) {
                ProcessVersionDTO processVersionDTO = processVersionDTOS.get(0);
                ProcessVersionContentDO pc = processVersionContentDaoService.queryByProcessVersionId(
                        processVersionDTO.getId()
                );

                if(ObjectUtil.isNull(pc)){
                    pc = new ProcessVersionContentDO();
                }
                String content = pc.getBackFile();
                if (StrUtil.isNotBlank(content)) {
                    SquareLakeLayout byIdAndVersion = JsonUtil.toBean(content, SquareLakeLayout.class);
                    processVersionContentDO.setBackFile(JsonUtil.toStr(byIdAndVersion));
                    processVersionContentDaoService.updateById(processVersionContentDO);
                }
            }
            versionDaoService.save(addRequest);
        } else {
            versionDaoService.updateById(addRequest);
        }
        return addRequest.getId();
    }

    @Override
    public Boolean setStatusFlag(ProcessVersionDO assertUserUse) {
        return versionDaoService.setStatusFlag(assertUserUse);
    }

    @Override
    public Boolean setLatestFlag(ProcessVersionDO assertUserUse) {

        ProcessVersionDO byId = versionDaoService.getById(assertUserUse.getId());
        if (ObjectUtil.isNull(byId)) {
            return Boolean.FALSE;
        }

        ProcessVersionDO processVersionDO = new ProcessVersionDO();
        processVersionDO.setProcessId(byId.getProcessId());
        List<ProcessVersionDTO> processVersionDTOS = versionDaoService.queryList(processVersionDO);
        if (CollUtil.isNotEmpty(processVersionDTOS)) {
            for (ProcessVersionDTO processVersionDTO : processVersionDTOS) {
                processVersionDTO.setLatestFlag(0);
                versionDaoService.setLatestFlag(BeanUtil.copyProperties(processVersionDTO, ProcessVersionDO.class));
            }
        }
        return versionDaoService.setLatestFlag(assertUserUse);
    }

    @Override
    public Boolean clearCache(ProcessVersionDO processVersionDO) {
//        processRepositoryManager.process().clearCache(processVersionDO.getOpenId());
        return Boolean.TRUE;
    }

    @Override
    public ProcessVersionDTO getByProcessIdAndVersion(ProcessVersionDO addRequest) {
        ProcessVersionDO processVersionDO = versionDaoService.queryByProcessIdAndVersion(
                addRequest.getProcessId(),
                addRequest.getProcessVersion()
        );
        return BeanUtil.copyProperties(processVersionDO, ProcessVersionDTO.class);
    }

    @Override
    public Boolean removeByProcessIdAndVersion(String processId, String processVersion) {
        return versionDaoService.removeByProcessIdAndVersion(processId,processVersion);
    }

    @Override
    public ProcessVersionDTO getById(ProcessVersionDO addRequest) {
        ProcessVersionDO byId = versionDaoService.getById(addRequest);
        return BeanUtil.copyProperties(byId, ProcessVersionDTO.class);
    }

    @Override
    public Boolean removeById(ProcessVersionDO removeByIdRequest) {
        ProcessVersionDO processVersionDO = versionDaoService.getById(removeByIdRequest.getId());
//        String openId = processVersionDO.getOpenId();

        boolean b = versionDaoService.removeById(removeByIdRequest.getId());
        //还要删除
//        processRepositoryManager.process().removeById(openId);
        processVersionContentDaoService.deleteByProcessVersionId(processVersionDO.getId());
        return Boolean.TRUE;
    }
}
